package com.mj.domain.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.mj.domain.util.HttpUtils;
import com.mj.domain.util.RegUtil;

/**
 * 域名查询
 * @author zhaominglei
 * @date 2015-3-29
 * 
 */
public class DomainQueryService extends BaseService {
	@SuppressWarnings("unused")
	private static final String TAG = DomainQueryService.class.getSimpleName();
	private static final String DOMAINQUERY_NET = "http://pandavip.www.net.cn/check/check_ac1.cgi"; //net
	
	@SuppressWarnings("serial")
	public List<String> domainsuffixList = new ArrayList<String>(){
		{
			add(".com");
			add(".cn");
			add(".xyz");
			add(".wang");
			add(".net");
			add(".我爱你");
			add(".top");
			add(".ren");
			add(".club");
			add(".org");
			add(".com.cn");
		}
	};
	
	public Map<String, String> getDomainQuery(String domainname, String domainsuffix) {
		domainname = domainname.replaceAll("http://", "").replaceAll("www.", "").replaceAll("www", "");
		if (domainname == null || domainname.equals("")) {
			return null;
		}
		if (domainsuffix == null || domainsuffix.equals("")) {
			return null;
		}
		Map<String, String> map = new LinkedHashMap<String, String>();
		StringBuilder queryURL = new StringBuilder(DOMAINQUERY_NET);
		queryURL.append("?domain=").append(domainname).append(domainsuffix);
		queryURL.append("&callback=jQuery17103543561662081629_").append(new Date().getTime());
		queryURL.append("&_=").append(new Date().getTime());
		String html = HttpUtils.doGetForNet(domainname, domainsuffix, queryURL.toString(), null);
		String conent = RegUtil.getMatchRegStr(html, "\\(\"(.+?)\"\\)");
		String[] contentArray = conent.split("\\|");
		for (int i = 0; (contentArray != null && contentArray.length > 0) && i < contentArray.length; i++) {
			String string = contentArray[i];
			
			if (i == 2) {
				String domainInfo = "";
				if ("210".equals(string)) {
					domainInfo = "尚未注册";
				} else {
					domainInfo = "已被注册";
				}
				map.put(domainname+domainsuffix, domainInfo);
				break;
			}
		}
		domainsuffixList.remove(domainsuffix);
		for (String suffix : domainsuffixList) {
			String searchURL = new String(DOMAINQUERY_NET);
			searchURL += "?domain="+domainname+suffix;
			searchURL += "&callback=jQuery17103543561662081629_"+new Date().getTime();
			searchURL += "&_="+new Date().getTime();
			html = HttpUtils.doGetForNet(domainname, suffix, searchURL, null);
			conent = RegUtil.getMatchRegStr(html, "\\(\"(.+?)\"\\)");
			contentArray = conent.split("\\|");
			for (int i = 0; (contentArray != null && contentArray.length > 0) && i < contentArray.length; i++) {
				String string = contentArray[i];
				if (i == 2) {
					String domainInfo = "";
					if ("210".equals(string)) {
						domainInfo = "尚未注册";
					} else {
						domainInfo = "已被注册";
					}
					map.put(domainname+suffix, domainInfo);
					break;
				}
			}
		}
		return map;
	}
}
